package com.qf.sl.youshare.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.qf.sl.youshare.Constants;
import com.qf.sl.youshare.R;
import com.qf.sl.youshare.model.Article;
import com.qf.sl.youshare.utils.FileCache;
import com.qf.sl.youshare.utils.ImageCache;
import com.qf.sl.youshare.utils.ImageLoader;

import java.util.LinkedList;

/**
 * Created by IDEA
 * User : SL
 * on  2015/2/20.
 * 21:11
 */
public class SheJiaoShareAdapter extends BaseAdapter {

    private LinkedList<Article> articles;
    private LayoutInflater inflater;

    public SheJiaoShareAdapter(Context context, LinkedList<Article> articles){
        this.articles = articles;
        if (context != null) {
            inflater = LayoutInflater.from(context);
        }else {
            throw new IllegalArgumentException("Context must not null");
        }
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (articles != null) {
            ret = articles.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (articles != null) {
            ret = articles.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        if (convertView != null) {
            ret = convertView;
        }else {
            ret = inflater.inflate(R.layout.item_shejiao_share, parent, false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();

        if (holder == null) {

            holder = new ViewHolder();

            holder.layoutBgHolder = (ImageView) ret.findViewById(R.id.shejiao_share_down_layout_bg);
            holder.titleHolder = (TextView) ret.findViewById(R.id.shejiao_share_title);
            holder.countsHolder = (TextView) ret.findViewById(R.id.shejiao_share_message_counts);

            ret.setTag(holder);

        }

        Article article = articles.get(position);

        String title = article.getTitle();
        holder.titleHolder.setText(title);

        String countComment = article.getCountComment();
        holder.countsHolder.setText(countComment);

        holder.layoutBgHolder.setImageResource(R.drawable.product_default);

        String photoUrl = article.getPhoto();

        if (photoUrl != null && photoUrl.length() > 0) {
            if (!photoUrl.startsWith("http://") && !photoUrl.startsWith("https://")){
                photoUrl = Constants.IMAGE_SERVER + photoUrl;
            }

            holder.layoutBgHolder.setTag(photoUrl);

            // 进行内存缓存的检查，如果有图片，直接加载
            ImageCache imageCache = ImageCache.getInstance();

            Bitmap bitmap = imageCache.getImage(photoUrl);
            // 如果内存中包含图片对象 Bitmap 那么就直接设置 ImageView
            if (bitmap != null) {
                holder.layoutBgHolder.setImageBitmap(bitmap);
            } else {
                // 不包含缓存，进行本地文件缓存的加载

                // TODO 加载本地文件内容
                FileCache fileCache = FileCache.getInstance();

                byte[] bytes = fileCache.getFileContent(photoUrl);

                if(bytes != null && bytes.length > 0){

                    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    holder.layoutBgHolder.setImageBitmap(bmp);

                    imageCache.putImage(photoUrl, bmp);

                }else {
                    // 就进行网络的加载
                    // 1. 开启异步任务
                    ImageLoader task = new ImageLoader(holder.layoutBgHolder);
                    task.execute(photoUrl);
                }
            }

        }


        return ret;
    }

    private static class ViewHolder{
        public ImageView layoutBgHolder;
        public TextView titleHolder;
        public TextView countsHolder;
    }

}
